package com.css.modules.lawRepo.controller;

import com.css.common.utils.PageUtils;
import com.css.common.utils.R;
import com.css.common.validator.ValidatorUtils;
import com.css.common.validator.group.AddGroup;
import com.css.common.validator.group.UpdateGroup;
import com.css.modules.lawRepo.entity.LawRepoUserEntity;
import com.css.modules.lawRepo.service.LawRepoUserService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: Chen Ao
 * @Date: 2020/2/18/018
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping("lawRepo/user")
@Api(value = "/user", description = "法律法规用户", tags = "法律法规用户")
public class LawRepoUserController {

    @Autowired
    private LawRepoUserService lawRepoUserService;

    @GetMapping("/list")
    @ApiOperation(value = "法律法规用户列表", notes = "返回法律法规用户列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "sidx", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序方式，如：asc、desc", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "法律法规用户名", dataType = "string", paramType = "query"),


    })
    //分页查询
    public R list(@ApiParam(name="params" ,hidden = true)@RequestParam Map<String, Object> param){

        PageUtils page = lawRepoUserService.queryPage(param);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
 //   @RequiresPermissions("lawRepo:lawRepoUser:info")
    public R info(@PathVariable("id") String id){
        LawRepoUserEntity lawRepoUser = lawRepoUserService.getById(id);
        return R.ok().put("lawRepoUser", lawRepoUser);
    }

    /**
     * 保存
     */

    @PostMapping("/save")
    @ApiOperation(value = "添加法律法规用户", produces = "application/json")
    public R save(@RequestBody LawRepoUserEntity lawRepoUser){
        if(StringUtils.isNotBlank((lawRepoUser.getId()))){
            ValidatorUtils.validateEntity(lawRepoUser, AddGroup.class);
            lawRepoUser.setId(null);
            lawRepoUser.setDelFlag("2");
        }

        lawRepoUserService.save(lawRepoUser);
        return R.ok();
    }


    /**
     * 修改
     */

    @ApiOperation(value = "修改用户积分", produces = "application/json")

    @PostMapping("/update/{id}")
    public R update(@PathVariable("id") String id, Integer point){
       /* ValidatorUtils.validateEntity(lawRepoUser, AddGroup.class);
        lawRepoUserService.updateById(lawRepoUser);*/
        LawRepoUserEntity user = lawRepoUserService.getById(id);
        if(user!=null){
            user.setPoint(point);
            lawRepoUserService.updateById(user);
        }
        return R.ok();
    }


    /**
     * 全部删除
     */

    @GetMapping("/deleteAll")
    @ApiOperation(value = "全部删除", produces = "application/json")
    public R deleteAll(){
        lawRepoUserService.commonUpdateDelFlagAll("1");
        return R.ok();
    }

    /**
     * 批量删除
     */

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除", produces = "application/json")
    @ApiImplicitParam(name = "mapIds", value = "ids", paramType = "body",dataType = "body")
    public R deleteBatch(@RequestBody Map<String, Object> mapIds){
        lawRepoUserService.commonUpdateDelFlagBatch("1", mapIds);
        return R.ok();
    }



}
